﻿@using RBS.Web.Controllers
@model RBS.Core.Model.Hotel.H_Hotels
@{
    ViewBag.Title = "Detail";
    Layout = "~/Views/Shared/_Layout.cshtml";
    var hId = Model.H_id;
    var uid = -1;
    if (Request.IsAuthenticated)
    {
        uid = Convert.ToInt32(Context.User.Identity.Name);
    } 
}
<div id="content">
    <div class="h1a">
        本地商户  @Html.Hidden("orderid", 0, new { value = @ViewBag.orderid })
        @Html.Hidden("IsGuest", 0, new { value = @ViewBag.IsGuest })
    </div>
    @{
        @Html.Partial("_HotelHead",Model);
    }
    <div id="mainContent">
        <dl id="CategoryUL" class="dtlcategdtl">
            <dd class="active" id="0">
                <a href="javascript:;">全部</a>
            </dd>
        </dl>

        <dl class="dtlorderby">
            <dd class="active"><a href="javascript:;">默认</a></dd>
            <dd><a href="javascript:;">价格</a></dd>
            <dd><a href="javascript:;">热度</a></dd>
            <dd><a href="javascript:;">评价数</a></dd>
            <dd><a href="javascript:;">好评率</a></dd>
        </dl>
        <div class="maindishlist">
            <ol id="CookbookUL">
            </ol>
        </div>

    </div>
    <div id="sideContent">
        <div class="mymenu">
            <h2>我的菜单</h2>
            <div class="menulist">
                <ul id="orderUL">
                </ul>
                <div class="total-warp">
                    <a href="javascript:void(0);" class="J_DeletCartData" onclick="DeleteOrderTemp(-1,2)">清空</a>
                    <dl class="price">
                        <dd class="item-count">合计数量：<b id="numcount"></b>份</dd>
                        <dd class="item-tit">合计金额：<b>¥</b> <strong id="priceCount"></strong></dd>
                        <dd class="item-label">@*节省：<b>¥</b> <strong>20</strong>*@</dd>
                        <dd class="price-other">
                            <p>&nbsp; </p>
                            1.菜单金额以实际消费为准；<p>&nbsp; </p>
                            2.时价商品将由店家进行计算；</dd>
                    </dl>
                    <a class="next-btn" href="#" id="BtnNextOrder">确认</a>
                </div>
            </div>
        </div>
    </div>
</div>
<script> 
   
    var cookbook;
    $(function () {
          GetCategory(@hId);
          GetCookbook(@hId,0);
          GetOrderTempDetail(@uid,$("#orderid").val());
          $("#BtnNextOrder").click(function() {    
             var url = "@Url.Action("OrderTemp", "Store")?hotelid=" + @hId+"&orderid="+$("#orderid").val();
             window.location = url; 
           });
    });
    //获取菜谱分类 
    function GetCategory(hid) {
        var url = "@Url.Action("GetCategory", "Store")?hid=" + hid; 
        $.ajax({
            url:url,
            type: 'Post',
            dataType: 'json',
            success: function (result) { 
                if (result == null)
                    return; 
                $.each(result.Data, function (index, row) { 
                    var template = $('#CategoryTemp').html(); //获取模版的html
                    var bookData = Mustache.render(template, row);//把每行的数据填充到模版得到html内容
                    $("#CategoryUL").append(bookData);
                });
                $("#CategoryUL dd").each(function () { 
                    $(this).click(function () {
                        $("#CategoryUL dd").removeClass("active");
                        $(this).addClass("active");
                        //alert($(this).attr("id")); 
                        $("#CookbookUL").empty();
                        GetCookbook(hid, $(this).attr("id")); 
                    }); 
                });  
            }
        }); 
    }
    //显示菜谱
    function GetCookbook(hid, cid) {
        var url = "@Url.Action("GetCookbook", "Store")?type=Cookbook&hid=" + hid + "&cid=" + cid; 
        $.ajax({
            url: url,
            type: 'Post',
            dataType: 'json',
            success: function (result) {
                if (result.Data.length==0) { 
                    var li = document.createElement('li');
                    var msg = document.createElement('div');
                    $(msg).attr("style", "float:right;width:60%");
                    $(msg).html("本类暂无菜单");
                    $(li).append(msg);
                    document.getElementById('CookbookUL').appendChild(li);
                    return;
                } 
                cookbook = result.Data;
                $.each(result.Data, function (index, row) { 
                    var template = $('#CookbookTemp').html(); //获取模版的html
                    var bookData = Mustache.render(template, row);//把每行的数据填充到模版得到html内容
                    $("#CookbookUL").append(bookData);
                });
                $("#CookbookUL p").each(function () { 
                    $(this).click(function () {  
                        if (@uid==-1) { 
                            var url = "@Url.Action("Index", "Login", new { redirectUrl = Request.Url })";
                            window.location = url;
                        }
                        else if ($("#orderid").val() == "0") {
                            Order();
                        }
                        else {
                            AddOrder($(this).attr('Id'), $(this).attr('Name'), $(this).attr('Price'), 1, $("#orderid").val());
                        }
                    }); 
                });   
            }
        });
    }   
    //添加到
    function AddOrder(a, b, c,d,e) {  
        var tempurl = "@Url.Action("OrderTempDetail", "Order", new { Area = "User" })";
        $.ajax({
            url: tempurl,
            cache: false,
            type: 'POST',
            dataType: "json", 
            data: { 
                CName: b,
                Price: c,
                HCId: a,
                Num: d,
                IsGuest: @ViewBag.IsGuest,
                UId:@uid,
                orderid:$("#orderid").val(),
                hid:@Model.H_id,
                hname:'@Model.H_Name'
            },
            success: function (result) {  
                if (result.Data == null)
                    return;  
                $("#orderUL").empty();
                var numCount = 0;
                var priceCount = 0; 
                $.each(result.Data, function (index, row) {
                    priceCount += row.Price*row.Num;
                    numCount += row.Num; 
                    var template = $('#OrderTemp').html(); //获取模版的html
                    var bookData = Mustache.render(template, row);//把每行的数据填充到模版得到html内容
                    $("#orderUL").append(bookData);
                }); 
                $("#priceCount").html(priceCount.toFixed(2));
                $("#numcount").html(numCount); 
            }
        });
    }
    //零时订单
    function GetOrderTempDetail(uid,oid) {
        var url = "@Url.Action("GetOrderTempDetail", "Order", new { Area = "User" })?uid=" + uid+"&oid="+oid; 
        $.ajax({
            url:url,
            type: 'Post',
            dataType: 'json',
            success: function (result) {  
                $("#orderUL").html("");
                if (result.Data == null)
                    return; 
                var numCount = 0;
                var priceCount = 0;
                $.each(result.Data, function (index, row) { 
                    priceCount += row.Price*row.Num;
                    numCount += row.Num; 
                    var template = $('#OrderTemp').html(); //获取模版的html
                    var bookData = Mustache.render(template, row);//把每行的数据填充到模版得到html内容
                    $("#orderUL").append(bookData);
                }); 
                $("#priceCount").html(priceCount.toFixed(2));
                $("#numcount").html(numCount); 
            }
        }); 
    } 
    function DeleteOrderTemp(a, b) {  
        var tempurl = "@Url.Action("DeleteOrderTempDetail", "Order", new { Area = "User" })";
        $.ajax({
            url: tempurl,
            cache: false,
            type: 'POST',
            dataType: "json", 
            data: {
                id:a,
                type:b 
            },
            success: function (result) {  
                $("#orderUL").html("");
                if (result == null)
                    return; 
                var numCount = 0;
                var priceCount = 0;
                $.each(result.Data, function (index, row) { 
                    priceCount += row.Price*row.Num;
                    numCount += row.Num; 
                    var template = $('#OrderTemp').html(); //获取模版的html
                    var bookData = Mustache.render(template, row);//把每行的数据填充到模版得到html内容
                    $("#orderUL").append(bookData);
                }); 
                $("#priceCount").html(priceCount);
                $("#numcount").html(numCount);
            }
        });
    }  
</script>
<!--菜谱模版-->
<script type="text/template" id="CategoryTemp">
    <dd id="{{Id}}"><a href="javascript:;">{{Name}}</a></dd>
</script>
<!--    -->
<script type="text/template" id="CookbookTemp">
    <li>
        <div class="mainimg">
            <img src="@(RBS.Common.ConfigHelper.GetConfigString("hotelWeb")){{DishImages}}" alt="{{Name}}" /></div>
        <h3>{{Name}}</h3>
        <dl class="score">
            <dd class="money">￥<strong>{{Price}}/{{Unit}}</strong></dd>
            <dd class="sales">@*销量：<b>215</b>*@ &nbsp;</dd>
        </dl>
        <p class="setu" id="{{Id}}" name="{{Name}}" price="{{Price}}"><a href="javascript:void(0);">点菜</a></p>
    </li>
</script>
<!--已点菜谱模版-->
<script type="text/template" id="OrderTemp">
    <li>
        <div class="food-count-del"><a href="javascript:void(0);" data-change="-1" onclick="DeleteOrderTemp({{HCId}},1)">×</a></div>
        <div class="food-count-name">{{CName}}</div>
        <div class="food-count-price">{{Price}}</div>
        <div class="food-count-reduce"><a href="javascript:void(0);" data-change="-1" onclick="AddOrder({{HCId}},'{{CName}}',{{Price}},-1);">-</a></div>
        <div class="food-count-num">{{Num}}</div>
        <div class="food-count-add"><a href="javascript:void(0);" data-change="1" onclick="AddOrder({{HCId}},'{{CName}}',{{Price}},1);">+</a></div>
    </li>
</script>
